package weaver.formmode.customjavacode.modeexpand.xfjt.duss;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.soa.workflow.request.RequestInfo;

import java.util.*;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-06-13
 * @Description: 2.登记提交后，在【发放台帐】生成每项物品的发放记录，扣减库存。
 * 3.扣除的【办公用品入库登记】里面那个记录，将信息写入明细表，便于后面反查。
 * 4.计算发放的物品总价格。
 * @ModifyLog:
 **/

public class WeaBgypfftzExpand extends AbstractModeExpandJavaCodeNew {
    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            //数据id
            int billid = -1;
            //模块id
            int modeid = -1;
            //模块id
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");
            if (requestInfo != null) {
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                if (billid > 0 && modeid > 0) {
                    /*
                     *@Description:
                     */
                    RecordSet rs = new RecordSet();
                    RecordSet rs1 = new RecordSet();
                    rs.writeLog("WeabgypfftzExpand>>>>>" + billid);
                    //插入主表
                    String sql = "select * from uf_bgypfftz where id=?";
                    rs.executeQuery(sql, billid);
                    rs.next();
                    //名称
                    String wpmc = Util.null2String(rs.getString("wpmc"));
                    String wpmcll = Util.null2String(rs.getString("wpmcll"));
                    //申领数量
                    int slsl = Util.getIntValue(rs.getString("sl"));
                    //发放时间
                    String ffsj = Util.null2String(rs.getString("ffsj"));
                    //申领人
                    String slr = Util.null2String(rs.getString("slr"));
                    //申领日期
                    String slrq = Util.null2String(rs.getString("slrq"));
                    String wplb = Util.null2String(rs.getString("wplb"));
                    double zj = 0;
                    int count;
                    int xslsl = slsl;
                    rs1.executeQuery("select id,ylysl,dj,rksj,(sl-isnull(ylysl,0)-isnull(ybfsl,0)) sysl " +
                            "from uf_bgyprkmx where wpmc=? and wplb=? and (sfsc is null or sfsc=1) and (sl-isnull(ylysl,0)-isnull(ybfsl,0))>0 " +
                            " order by rksj", wpmcll,wplb);
                    while (rs1.next()) {
                        int id = Util.getIntValue(rs1.getString("id"));
                        int sysl = Util.getIntValue(rs1.getString("sysl"),0);
                        double dj = Util.getDoubleValue(rs1.getString("dj"),0);
                        rs1.writeLog(">>>>id="+id+",sysl="+sysl+",slsl="+slsl);
                        count = xslsl - sysl;
                        if (count > 0) {
                            xslsl = xslsl - sysl;
                            zj += sysl * dj;
                            rs1.executeUpdate("insert into uf_bgyprkmx_dt1(mainid,ffsj,slr,slrq,ffdjjl,slsl) " +
                                    "values (?,?,?,?,?,?)",id,ffsj,slr,slrq, billid,sysl);
                            rs1.executeUpdate("update uf_bgyprkmx set ylysl=isnull(ylysl,0)+"+sysl+" where id=?",id);
                        } else {
                            zj += xslsl * dj;
                            rs1.executeUpdate("insert into uf_bgyprkmx_dt1(mainid,ffsj,slr,slrq,ffdjjl,slsl) " +
                                    "values (?,?,?,?,?,?)",id,ffsj,slr,slrq, billid,xslsl);
                            rs1.executeUpdate("update uf_bgyprkmx set ylysl=isnull(ylysl,0)+"+xslsl+" where id=?",id);
                            break;
                        }
                    }
                    rs.executeUpdate("update uf_bgypfftz set zj=isnull(zj,0)+"+zj+" where id=?",billid);
                    rs.executeUpdate("update uf_bgypkc set kc=kc-"+slsl+" where id=?",wpmc);
                }
            }
        } catch (Exception e) {
            result.put("errmsg", "自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }
    /**
     * @Description: 将数据插入表单建模
     */
    public static int insertModelData(String tablename, int userid, Map map) {
        int dataid = 0;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        String uuid = UUID.randomUUID().toString();
        int modeid = getFormmodeId(tablename);
        String insSql = "insert into " + tablename
                + "(uuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values(?,?,?,?,?)";
        Object[] params = {uuid, userid, TimeUtil.getCurrentDateString(), TimeUtil.getOnlyCurrentTimeString(), modeid};
        if (rs.executeUpdate(insSql, params)) {
            rs1.executeQuery("select id from " + tablename + " where uuid=?", uuid);
            rs1.next();
            dataid = Util.getIntValue(rs1.getString("id"));
            if (dataid > 0) {
                //遍历数据 进行update
                String updatesql = "update " + tablename + " set ";
                Set<String> keySet = map.keySet();
                for (String key : keySet) {
                    updatesql += key + "='" + map.get(key).toString() + "',";
                }
                if (updatesql.endsWith(",")) {
                    updatesql = updatesql.substring(0, updatesql.length() - 1);
                    updatesql += " where id=" + dataid;
                    rs.executeUpdate(updatesql);
                }
                //进行权限重构

                getNewModeRight(userid, modeid, dataid);
            }
        }
        return dataid;
    }
    /**
     * @Description: 建模权限重构
     */
    public static void getNewModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight(true);
        modeRightInfo.editModeDataShare(userid, formmodeid, id);
    }

    /**
     * @Description: 获取formmodeid
     */
    public static int getFormmodeId(String tableName) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select m.id from modeinfo m left join WORKFLOW_BILL b on m.formid = b.id where b.tablename = ?", tableName);
        rs.next();
        return Util.getIntValue(rs.getString("id"));
    }

}
